<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <title>Running many technical analysis functions on large multi-dimentional decimal
      array</title>
   <style type="text/css">
      body
      {
         padding: 0;
         margin: 1em;
         font-family: Verdana;
         font-size: 10pt;
         font-weight: bold;
      }
      #container
      {
         margin: 70px;
         width: 400px;
         text-align: center;
      }
      #progressbar
      {
         position: relative;
         width: 400px;
         height: 25px;
         border: 1px solid black;
         overflow: hidden;
         text-align: left;
      }
      #progressbar div
      {
         float: left;
         background: #316AC5;
         width: 0;
         height: 100%;
      }
      #percentComplete
      {
         width: 400px;
         text-align: center;
         margin: 0px;
         padding: 0px;
      }
      #statusMessage
      {
         width: 425px;
         text-align: left;
      }
      .complete
      {
         color: Green;
      }
   </style>
</head>
<body>
   <div id="container">
      <div id="progressbar">
         <div>
         </div>
      </div>
      <span id="percentComplete">0%</span>
      <p id="statusMessage">
         Loading test...</p>
   </div>

   <script src="js-TA.js" type="text/javascript"></script>

   <script type="text/javascript" language="javascript">
      window.onload =
        (function() {

           var i, length, innerLength, iterations, timeoutLength, timeoutFreq, data, el, elParent, start;

           // Initialize a few things...
           length = 800;
           innerLength = 201;
           iterations = 1;
           timeoutLength = 20;
           timeoutFreq = 2000;

           data = [];
           el = document.getElementById("progressbar").getElementsByTagName('div')[0];
           percentEl = document.getElementById("percentComplete");

           function setStatus(msg, appendLine) {
              if (appendLine && appendLine == true) {
                 msg = document.getElementById('statusMessage').innerHTML + "<br />" + msg;
              }
              document.getElementById('statusMessage').innerHTML = msg;
           }

           // Setup an array of random integers...
           setStatus('Creating ' + length + ' X ' + innerLength + ' data array...');

           for (i = 0; i < length; i++) {
              data[i] = { series: [] };
              var ii = 0;

              (function() {
                 var start;
                 start = new Date().getTime();

                 for (; ii < innerLength; ii++) {
                    data[i].series[ii] = TA.Helpers.roundDecimal(Math.floor(Math.random() * length, 3));
                    if (new Date().getTime() - start > timeoutFreq) {
                       ii++;
                       setTimeout(arguments.callee, timeoutLength);
                       break;
                    }
                 }
              })();
           }

           function taTransform(progressFn) {
              i = 0;
              (function() {
                 var start;
                 start = new Date().getTime();

                 for (; i < length; i++) {
                    var series = data[i].series;
                    with (data[i]) {
                       linR5 = TA.LinearReg(series.slice(0, 40), 5);
                       //linR10 = TA.LinearReg(series.slice(0, 82), 10);
                       linR20 = TA.LinearReg(series.slice(0, 40), 20);
                       //linR40 = TA.LinearReg(series.slice(0, 82), 40);
                       //linR80 = TA.LinearReg(series.slice(0, 82), 80);

                       emaOfLinR5 = TA.EMAverage(linR5.slice(0, 25), 3);
                       //emaOfLinR10 = TA.EMAverage(linR10.slice(0, 12), 8);
                       emaOfLinR20 = TA.EMAverage(linR20.slice(0, 40), 3);
                       //emaOfLinR40 = TA.EMAverage(linR40.slice(0, 42), 8);
                       //emaOfLinR80 = TA.EMAverage(linR80.slice(0, 82), 3);

                       diffEma1Over2 = TA.Helpers.roundDecimal(TA.Helpers.percentDiff(emaOfLinR5[0], emaOfLinR20[0]), 3);
                       //diffEma2Over3 = TA.Helpers.roundDecimal( TA.Helpers.percentDiff(emaOfLinR20, emaOfLinR80), 3);
                    }
                    if (new Date().getTime() - start > timeoutFreq) {
                       i++;
                       setTimeout(arguments.callee, timeoutLength);
                       break;
                    }
                 }
                 progressFn(i, length);
              })();
           }

           setStatus('Running TA calculations on ' + length + ' X ' + innerLength + ' data array...');
           start = new Date().getTime();
           taTransform(function(value, total) {
              el.style.width = (100 * value / total) + "%";
              percentEl.innerHTML = (100 * value / total) + "%";
              if (value >= total) {
                 percentEl.className = 'complete';
                 setStatus("Completed with total duration: " + ((new Date().getTime() - start) / 1000) + " seconds", true);
              }
           });

        });
   </script>

</body>
</html>
